package org.summerframework.core.check;


import org.summerframework.core.base.result.DefaultResultCode;
import org.summerframework.core.util.SpringContextUtil;

import javax.validation.ConstraintViolation;
import javax.validation.Validator;
import java.util.Set;

/**
 * @author liaorongbiao
 * @version 1.0
 * @date 2019-06-25 15:58
 **/
public class ValidationUtils {

    public static <T> void validate(T obj, Class<?>... groups) {
        Set<ConstraintViolation<T>> constraintViolations = SpringContextUtil.getBean(Validator.class).validate(obj, groups);
        // 抛出检验异常
        if (constraintViolations.size() > 0) {
            throw DefaultResultCode.ILLEGAL_ARGUMENT_CHECK.format(constraintViolations.iterator().next().getMessage());
        }
    }
}
